package com.danieldura.highrise.data
{
	import com.adobe.utils.DateUtil;
	
	import flash.events.Event;
	
	[Bindable]
	public class Case implements HighriseData
	{
		public var id:uint;
		public var closedAt:Date;
		public var name:String;
		public var createdAt:Date;
		public var updatedAt:Date;
		public var visibleTo:String;
		public var ownerId:int;
		public var groupId:int;
		public var authorId:int;
		
		[Bindable(event="avatarUrlChange")]
		public function get avatarUrl():String
		{
			var addZeros:int = 8 - id.toString().length;
			var idString:String = id.toString();
			
			for( var i:int = 0; i < addZeros; i++ )
				idString = "0" + idString;
				
			return "https://asset0.highrisehq.com/avatars/kase/" + idString.substr( 0, 4 ) + "/" + idString.substr( 4, 4 ) + "-large.png";
		}
	
		public function parse( data:XML ):void
		{
			id = data.id;
			name = data.name;
			visibleTo = data["visible-to"];
			ownerId = data["owner-id"];
			groupId = data["group-id"];
			authorId = data["author-id"];
			
			if( data["closed-at"] != "" ) closedAt = DateUtil.parseW3CDTF( data["closed-at"] );
			if( data["created-at"] != "" ) createdAt = DateUtil.parseW3CDTF( data["created-at"] );
			if( data["updated-at"] != "" ) updatedAt = DateUtil.parseW3CDTF( data["updated-at"] );
			
			dispatchEvent( new Event("avatarUrlChange") );
		}
		
		public function toXML():XML
		{
			var data:XML = new XML( <kase></kase> );
			data.id = id;
			data.name = name;
			data["visible-to"] = visibleTo;
			data["group-id"] = groupId;
			return data;
		}
		
		public function toXMLString():String
		{
			return toXML().toXMLString();			
		}		
	}
}

/*
<kase>
  <id type="integer">1</id>
  <closed-at type="datetime"></closed-at>
  <name>A very important matter</name>
  <created-at type="datetime"></created-at>
  <updated-at type="datetime">2007-03-19T22:34:22Z</updated-at>
  <visible-to>#{Everyone || Owner || NamedGroup}</visible-to>
  <owner-id type="integer">#{ user_id -- requires visble-to is "Owner"}</owner-id>
  <group-id type="integer">#{ group_id -- requires visble-to is "NamedGroup"}</group-id>
  <author-id type="integer">3</author-id>
</kase>
*/